package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketIssue;
import com.cskaoyan.market.service.MarketIssueService;
import com.cskaoyan.market.util.HttpUtil2;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RequestMapping("/admin/issue")
@RestController
public class MarketIssueController {

    // 父接口接收
    @Autowired
    MarketIssueService marketIssueService;


    @PostMapping("/update")
    @RequiresPermissions("admin:issue:update")
    @RequiresPermissionsDesc(menu = {"商场管理", "通用问题"},button = "更新")
    public Object update(HttpServletRequest req) throws IOException {
        String requestBody = HttpUtil2.getRequestBody(req);

        // String id = JacksonUtil.parseString(requestBody, "id");
        Integer id = JacksonUtil.parseInteger(requestBody, "id");
        String question = JacksonUtil.parseString(requestBody, "question");
        String answer = JacksonUtil.parseString(requestBody, "answer");
        String addTime = JacksonUtil.parseString(requestBody, "addTime");
        String updateTime = JacksonUtil.parseString(requestBody, "updateTime");
        Boolean deleted = JacksonUtil.parseBoolean(requestBody, "deleted");

        // 判空
        MarketIssue marketIssue = marketIssueService.updateIssue(id, question, answer, addTime, updateTime, deleted);

        return ResponseUtil.ok(marketIssue);
    }

    @PostMapping("/delete")
    @RequiresPermissions("admin:issue:delete")
    @RequiresPermissionsDesc(menu = {"商场管理", "通用问题"},button = "删除")
    public Object delete(HttpServletRequest req) throws IOException {
        String requestBody = HttpUtil2.getRequestBody(req);
        String id = JacksonUtil.parseString(requestBody, "id");
        String answer = JacksonUtil.parseString(requestBody, "answer");
        String addTime = JacksonUtil.parseString(requestBody, "addTime");
        String updateTime = JacksonUtil.parseString(requestBody, "updateTime");
        String deleted = JacksonUtil.parseString(requestBody, "deleted");

        int returnCode = marketIssueService.deleteIssue(id);

        // {"errno":0,"errmsg":"成功"}
        if (returnCode == 0) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.fail();
        }

    }


    @PostMapping("/create")
    @RequiresPermissions("admin:issue:create")
    @RequiresPermissionsDesc(menu = {"商场管理", "通用问题"},button = "新增")
    public Object create(HttpServletRequest req) throws IOException {
        // 获取请求体
        String requestBody = HttpUtil2.getRequestBody(req);
        String question = JacksonUtil.parseString(requestBody, "question");
        String answer = JacksonUtil.parseString(requestBody, "answer");

        // 判空
        if (StringUtils.isEmpty(question)) {
            Object o = ResponseUtil.fail(605, "问题不能为空");
            return JacksonUtil.writeValueAsString(o);
        } else if (StringUtils.isEmpty(answer)) {
            Object o = ResponseUtil.fail(605, "回复不能为空");
            return JacksonUtil.writeValueAsString(o);
        }

        // 新增问题
        MarketIssue marketIssue = marketIssueService.createIssue(question, answer);

        return ResponseUtil.ok(marketIssue);
    }

    @GetMapping("/list")
    @RequiresPermissions("admin:issue:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "通用问题"},button = "查询")
    public Object list(HttpServletRequest req) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String question = req.getParameter("question");

        List<MarketIssue> marketIssueList = marketIssueService.list(page, limit, sort, order, question);

        return ResponseUtil.okList(marketIssueList);
    }
}
